import {generateHS256Token} from "jwt-hs256";
import moment from "moment";
function formatpostion(_arr){
    let cureenArr=[];
    _arr.map(o=>{
        cureenArr.push(o.split(',')) ;
    });
    return cureenArr
}
export default  {
    //一维数组空间数据转化为tree结构
    spaceTree(arr=[],title=`name`,children='child',id=''){
        let _id=id||_.get(arr,'0.id');
        let tree=(arr)=>{
            arr.map(item=>{
                item.title=item[title];
                item.expand=true;
                item.selected=item.id==_id?true:false;
                item[`children`]=item[children];
                if((_.get(item,'children')||[]).length>0){
                    tree(item.children);
                }
            })
        }
        tree(arr);
        return arr;
    },
    //
    treeToArray(arr=[],children='children'){
        let result=[];
        let getArray=(_arr)=>{
            _arr.map(item=>{
                result.push(item);
                if(item[children] instanceof Array){
                    getArray(item[children])
                }
            })
        }
        getArray(arr);
        return result;
    },
    getDeviceName(device_type,leve,icon_level) {//获取设备图标名称
        if(icon_level==0){
            return  'device_type'
        }

        leve=leve?leve:1;
        if(typeof(device_type)!='string'){
            return  'device_type'
        }else if(device_type.search('device_type')==-1){
            return  'device_type'
        }else{
            return device_type.substring(0,11+(leve-1)*4);
        }

    },
    subText(str, length){//截取文字
        if (str.length === 0) {
            return '';
        }
        if (str.length > length) {
            return str.substr(0, length) + "...";
        } else {
            return str;
        }
    },
    getStatus(statuCode){//通过状态码来获取状态名称
        let name='正常';
        switch (statuCode){
            case 'S03_001':
                name='离线'
                break;
            case 'S03_002':
                name='异常'
                break;
            case 'S03_003':
                name='安全警告'
                break;
            case 'S03_004':
                name='联动情况'
                break;
            case 'S03_005':
                name='工作运行'
                break;
            default :
                break;

        }
        return name;
    },
    getjwt_abk(appid){//获取jwt key secret 每个项目都是独立的
        let app_key='ZVx2CfGgP'
        let app_secret= 'bc72e5b1b1344b6852cda04dc1d06d22';
        let obj={
            iss:app_key,
            iat:Math.ceil(new Date().getTime()/1000),
            exp:Math.ceil(new Date().getTime()/1000)+2*24*3600
        }
        let ____= appid?{ user_id:appid, ...obj}:obj;
        console.log(____,'addd','appid');
        let token = generateHS256Token(____,app_secret);
        return {jwt:token}
    },
    getjwt(user_id=''){
        let app_key='aGfeftHr8Iko'
        let app_secret= 'tGmefBHr8Ixt';
      return   generateHS256Token({
            iss: app_key,
            exp: Math.floor(new Date().getTime()/1000)+ 24*60*60,
            iat: Math.floor(new Date().getTime()/1000),
            user_id:user_id
        }, app_secret)
    },
    //时间数组组件中的时间格式化(arr)
    changeTimeArray(arr=[],fromat,errMsg=''){
        return arr.map(item=>{
            return   this.changeTime(item,fromat,errMsg);
        }).filter(o=>o);
    },
    changeTime(str,fromat='YYYY-MM-DD HH:mm:ss',errMsg='-'){//时间格式化
        // console.log(str,'str====', moment(str).format(fromat));
        if(!str){
            return errMsg
        }
        if(moment(str).format(fromat)){
            return  moment(str).format(fromat)
        }else{
            return errMsg;
        }
    },
    /**
     * 深合并对象
     * @param obj1
     * @param obj2
     */
    deepMerge(obj1, obj2) {
        let key;
        for(key in obj2) {
            // 如果target(也就是obj1[key])存在，且是对象的话再去调用deepMerge，否则就是obj1[key]里面没这个对象，需要与obj2[key]合并
            obj1[key] = obj1[key] && obj1[key].toString() === "[object Object]" ?
                this.deepMerge(obj1[key], obj2[key]) : obj1[key] = obj2[key];
        }
        return obj1;
    },
    formatTree(arr=[],children='children',title='title',key='key',checked=true,expand=true,){
        let tree=(arr)=>{
            arr.map(item=>{
                item['title']=item[title];
                item['label']=item[title];
                item[`value`]=item[key];
                item[`children`]=item[children];
                item.checked=checked;
                item.expand=expand;
                if((_.get(item,'children')||[]).length>0){
                    tree(item.children);
                }

            })
        }
        tree(arr);
        // console.log(arr,'===lable==');
        return arr;
    },
    formatpostion(arr){
        return formatpostion(arr)
    },
    getTreeElm(arr=[],key='key'){
        let elms=[];
        let tree=(arr)=>{
            arr.map(item=>{
                elms.push(item[key]);
                if((_.get(item,'children')||[]).length>0){
                    tree(item.children);
                }
            })
        }
        tree(arr);
        return elms;
    },
    fromatMapInfo(arr=[]){//格式化线条路径
        arr.map(item=>{
            let _arr=(item.position&&JSON.parse(item.position)||[]);
            item.path=formatpostion(_arr);
        })
        return arr
    },
    getClassNamme(item){

        // _.get(item,'offlineStatus')&&classname.push('offlineStatus');
        // _.get(item,'faultStatus')&&classname.push('faultStatus');
        // _.get(item,'abnormalStatus')&&classname.push('abnormalStatus');
        // _.get(item,'normalStatus')&&classname.push('normalStatus');
        switch (_.get(item,'deviceStatus'))  {
            case 2:
                return 'faultStatus';
                break;
            case 3:
                return 'abnormalStatus';
                break;
            case 4:
                return 'offlineStatus';
                break;
            default:
                return 'normalStatus';
        }


    },
    getPoints(arr=[]){//格式化设备落点
        let devicePoints=[];
        console.log(this,'=====this===');
        arr.map(item=>{
            (_.get(item,'device')||[]).map(o=>{
                devicePoints.push({...o,address:item.address,orgName:item.orgName,hlName:item.name,projectName:item.projectName,className:this.getClassNamme(o),hlId:item.id,gps_lng:(_.get(o,'position')||'').split(',')[0],gps_lat:(_.get(o,'position')||'').split(',')[1]})
            })
        })
        // console.log(devicePoints,'devicePoints===')
        return devicePoints;
    },
}
